layui.use(['form', 'table'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

    table.render({
        elem: '#currentTableId',
        url: '/Order/list',
        toolbar: '#toolbarDemo',
        method:'post',
        contentType: 'application/json',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [[
            {type: "checkbox", width: 50},
            {field: 'id', width: 80, title: 'ID', hide:true},
            {field: 'shop',  title: '店铺名称'},
            {field: 'order_num',  title: '原始单号'},
            {field: 'dealer',  title: '分销商', sort: true},
            {field: 'ship_conditions',  title: '发货条件'},
            {field: 'order_type',  title: '订单类别'},
            {field: 'mem_name',  title: '收件人'},
            {field: 'mem_phone',  title: '手机'},
            {field: 'detailaddress',  title: '地址'},
            {field: 'goods',  title: '货品名称'},
            {field: 'goods_sku',  title: '规格名称' , hide:true},
            {field: 'goods_code',  title: '商家编码'},
            {field: 'sum_goods', title: '货品数量'},
            {field: 'supply_price',  title: '供货价'},
            {field: 'sum_goods_price',  title: '货品总价'},
            {field: 'total_order',  title: '应收合计'},
            {field: 'ware_house',  title: '仓库名称', sort: true},
            
            {field: 'offers',  title: '优惠金额'},
            {field: 'postage',  title: '邮费'},
            {field: 'remark',  title: '客服备注'},
            {title: '操作',  toolbar: '#currentTableBar', align: "center"}
        ]],
        limits: [100, 500, 1000, 3000],
        limit: 100,
        page: true,
        skin: 'line'
    });

    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        var result = JSON.stringify(data.field);
        layer.alert(result, {
            title: '最终的搜索信息'
        });

        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    /**
     * toolbar监听事件
     */
    table.on('toolbar(currentTableFilter)', function (obj) {
        if (obj.event === 'add') {  // 监听添加操作
            var index = layer.open({
                title: '创建订单',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: 'Order-add.html',
                end: function(){
                    table.reload('currentTableId');
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        } else if (obj.event === 'delete') {  // 监听删除操作
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;

            // layer.alert(JSON.stringify(data));
            layer.confirm("确认删除吗？", {
                title: '确认',
                end: function () {
                },
            }, function (index) {
                
                $.ajax({
                    url: "/Order/del",
                    type: "post",
                    data: JSON.stringify(data),
                    datatype: "json",
                    success: function () { 
                        table.reload('currentTableId');
                    },
                });
                layer.close(index);
            });
        } else if (obj.event === 'import') {  // 监听导入操作
            var index = layer.open({
                title: '导入订单',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: './Order-import.html',
                end: function(){
                    table.reload('currentTableId');
                },
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        }
    });

    //监听表格复选框选择
    table.on('checkbox(currentTableFilter)', function (obj) {
        console.log(obj)
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {

            var index = layer.open({
                title: '修改订单',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: './page/table/edit.html',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        }
    });

});